#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "nanfeiManager.h"
#include "../../fep/cli_main.h"
#include "../../fep/xml_conf.h"
#include "../../fep/wrapperjson.h"
#include "../../fep/cmd.h"
#include "../../fep/buf_misc.h"
static I32 moduleId = 0;
static I32 packNanfeiHb(I8 *outData, int8_t mobileState, int8_t unicomState)
{
    I32 len = 0;
    len = sprintf(outData, "%04d%12s0000%d%d", CMD_NANFEI_QUERY_EQUIT, dev_conf.dev.devNum, mobileState, unicomState);
    return len;

}

static I32 packNormalRsp(I32 code, I8 *outData, uint8_t result)
{
    I32 len = 0;
    len = sprintf(outData, "%04d%12s0000%d",  code, dev_conf.dev.devNum, result); 
    return len;
}

static void packSnifferCmd(I8 *input, I32 *inLen)
{
    msgConversion(CMD_CTRL, CT_RESRART, NULL, input, inLen);
    cli_app_log(CLI_LOG_INFO, "nanfei pack sniffer command \n");
}


static void packRestartCmd(I8 *input, I32 *inLen)
{
    msgConversion(CMD_CTRL, CT_RESRART, NULL, input, inLen);
    cli_app_log(CLI_LOG_NOTICE, "CT_RESTART\n");    
}


static void packSetWirelessCmd(uint8_t slotId, char *data, I32 len, I8 *input, I32 *inLen)
{
    WirelessConfig wConfig;
	memset(&wConfig, 0x0, sizeof(WirelessConfig));
    wConfig.wirelessSupported[slotId] = 1;
    I8 freq[4] = {0};
    int i = 0;
    gsmRadioConfig* gsm = NULL;
    wConfig.capGsm.isSupported = 1;			
	wConfig.capGsm.enable = 1;
    if (slotId == GSM_CELL_START)
    {
        gsm = &wConfig.capGsm.gsm[0];
    }
    else
    {
        gsm = &wConfig.capGsm.gsm[1];        
    }
    for ( i= 0; i < len && i < 4; i++)
    {   
        if (data[i] == ' ')
        {
            break;
        }
        freq[i] = data[i]; 
    }
    gsm->bcc = atoi(freq);
    cli_app_log(CLI_LOG_INFO, "set gsm %d bcc is %s \n", slotId, freq);
    msgConversion(CMD_CONFIG,CFG_WIRELESS, (I8 *)&wConfig, input, inLen);
}

void handleMsgFromNanfei(char *msgBuf, int msgLen, char *input, int *inLen)
{
    *inLen = 0;
    cli_app_log(CLI_LOG_INFO, "rcv nanfei msgLen %d, msg %s \n", msgLen, msgBuf);
    if (msgLen < NANFEI_MSG_HEAD_LENGTH)
    {
        cli_app_log(CLI_LOG_WARNING, "MSG is too short \n");
        return;
    }
    I8 msgCode[8] = {0};
    memcpy(msgCode, msgBuf, 4);
    I8 moduleIdTmp[8] = {0};
    memcpy(moduleIdTmp, (msgBuf + 16), 4);
    cli_app_log(CLI_LOG_INFO, "msg code is %d module %d \n", atoi(msgCode), atoi(moduleIdTmp));
    moduleId = atoi(moduleIdTmp);
    switch(atoi(msgCode))
    {
        case CMD_NANFEI_QUERY_EQUIT:
            break;

        case CMD_NANFEI_SET_EQUIPTNAME:
            break;

        case CMD_NANFEI_SET_FREQUENCY:
            packSetWirelessCmd((atoi(moduleIdTmp)==1?GSM_CELL_START:GSM_CELL_END), (msgBuf + 20), (msgLen - 20), input, inLen);
            break;

        case CMD_NANFEI_SET_TXPOWER:
            break;

        case CMD_NANFEI_SET_RESTART:
            packRestartCmd(input, inLen);
            break;

        case CMD_NANFEI_SET_AUTOCELLSEARCH:
            packRestartCmd(input, inLen);
            break;

        default:
            cli_app_log(CLI_LOG_WARNING, "not support code %d \n", msgCode);
            break;

    }
}

static void handleOtherMsg(I32 type, I32 code, I8 *indata, I8 *outdata, I32* len)
{
	DevStaus *devStatus = NULL;
	cli_app_log(CLI_LOG_INFO, "msg code is %d \n", code);
	switch(code)
	{
	 	case HT_SRV_STATUS_OK:	
		 	break;
	 	case SEX_REG_SUCCESS:
		 	break;

        case DEVICE_REQ_TIMESYNC:
            break;

	 	case DEVICE_STATUS:
		 	devStatus = (DevStaus *)indata;
			*len = packNanfeiHb(outdata, (devStatus->bbu[GSM_CELL_START].active==1?1:0), (devStatus->bbu[GSM_CELL_END].active==1?1:0));
		 	break;
		case SNF_RESULT:
			break;

        case BLACKLIST_ALERT:
            break;     
		default:
			cli_app_log(CLI_LOG_NOTICE, "unsupported command code failed\n");			
			break;
     }

}

static void handleCellConfigMsg(I32 type, I32 code, I32 result, I8 *indata, I8 *outdata, I32* len)
{
    switch(code)
    {
        case CFG_WIRELESS:
            *len = packNormalRsp(CMD_NANFEI_SET_FREQUENCY, outdata, (result == RST_SUCESS)?NANFEI_RST_SUCESS:NANFEI_RST_FAILD);
            break;
        default:
            break;

    }
}

static void handleCellControlMsg(I32 type, I32 code, I32 result, I8 *indata, I8 *outdata, I32* len)
{
    switch(code)
    {
        case CT_RESRART:
            *len = packNormalRsp(CMD_NANFEI_SET_FREQUENCY, outdata, (result != RST_FAILED)?NANFEI_RST_SUCESS:NANFEI_RST_FAILD);
            break;

        case CT_SNIFFER:
             *len = packNormalRsp(CMD_NANFEI_SET_FREQUENCY, outdata, (result != RST_FAILED)?NANFEI_RST_SUCESS:NANFEI_RST_FAILD);
            break;

    }

}

void sendMsgToNanfei(const char* buf, ssize_t len, char* output, int * outlen)
{
	cli_app_log(CLI_LOG_DEFAULT, "Sending Json message: %s\n", buf);
    
    //todo, decoder json string
    I32 cmdType;
	I32 cmdCode;
    I32 result;
	I8  outdata[65535];

    if(rspMsgConversion(&cmdType, &cmdCode, &result, (I8 *)buf, outdata, len))
    {
        switch(cmdType)
        {
            case CMD_OTHERS:
				handleOtherMsg(cmdType, cmdCode, outdata, output, outlen);
 				break;
			case CMD_CONFIG:
				handleCellConfigMsg(cmdType, cmdCode, result, outdata, output, outlen);
				break;
			case CMD_CTRL:
				handleCellControlMsg(cmdType, cmdCode, result, outdata, output, outlen);				
				break;
			default:
				cli_app_log(CLI_LOG_ERROR, "not support command type: %s\n", cmdType);
				break;
        }
    }

}
